package com.demo.lifeinfo.common.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.demo.lifeinfo.R;
import com.demo.lifeinfo.app.XBaseActivity;
import com.demo.lifeinfo.entity.City;
import com.demo.lifeinfo.entity.User;
import com.flyco.roundview.RoundTextView;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.google.android.material.appbar.AppBarLayout;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnInputConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FetchUserInfoListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * created by Raymond
 * on 2019-05-05
 */
public class EditPersonalInfoAc extends XBaseActivity {
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.toolbar_right_btn)
    RoundTextView toolbarRightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.modfiy_nickname)
    TextView modfiyNickname;
    @BindView(R.id.modfiy_city)
    TextView modfiyCity;
    @BindView(R.id.modfiy_sex)
    TextView modfiySex;
    @BindView(R.id.edit_xinqing)
    EditText editXinqing;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    String text;
    List<City> data;

    String mName, mCity, mSex;
    List<String> hobbys;

    BaseQuickAdapter<String, BaseViewHolder> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_info;
    }

    @Override
    protected void initView() {
        toolbarRightBtn.setVisibility(View.VISIBLE);
        toolbarRightBtn.setText("保存");
        hobbys = new ArrayList<>();
        try {
            InputStream inputStream = getAssets().open("city.txt");
            text = getString(inputStream).trim();
            data = JSON.parseArray(text, City.class);

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("xiaoqiang", e.getMessage());
        }
        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_hobby_white) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.item, item);
            }
        };
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {

    }

    public static String getString(InputStream inputStream) {
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer("");
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


    @OnClick({R.id.toolbar_back, R.id.textView3, R.id.toolbar_right_btn})
    public void onViewClicked2(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                onBackPressed();
                break;
            case R.id.textView3:
                break;
            case R.id.toolbar_right_btn:
                save();
                break;
        }
    }

    private void save() {

        User user = BmobUser.getCurrentUser(User.class);
        if (!TextUtils.isEmpty(mCity))
            user.setCity(mCity);
        if (!TextUtils.isEmpty(mName))
            user.setNickeName(mName);
        if (!TextUtils.isEmpty(editXinqing.getText().toString()))
            user.setMotto(editXinqing.getText().toString());
        if (!TextUtils.isEmpty(mSex))
            user.setSex(mSex);
        if (!hobbys.isEmpty())
            user.setHobby(hobbys);
        showProgressDialog();
        user.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    dimssProgressDialog();
                    showError("保存成功！");
                    onBackPressed();
                } else {
                    showError(e.getMessage());
                }

            }
        });
    }

    @OnClick({R.id.modfiy_nickname, R.id.modfiy_city, R.id.btn_hobby, R.id.modfiy_sex})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.modfiy_nickname:
                new XPopup.Builder(EditPersonalInfoAc.this).asInputConfirm("修改昵称", "请输入你想修改的昵称", new OnInputConfirmListener() {
                    @Override
                    public void onConfirm(String text) {
                        modfiyNickname.setText("昵称：  " + text);
                        mName = text;
                    }
                }).show();
                break;
            case R.id.modfiy_city:
                if (data == null || data.size() == 0) {
                    showError("程序错误!");
                    return;
                }
                String[] cityData = new String[data.size()];
                for (int i = 0; i < data.size(); i++) {
                    cityData[i] = data.get(i).name;
                }
                new XPopup.Builder(EditPersonalInfoAc.this)
                        .asBottomList("选择城市", cityData, new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                modfiyCity.setText("城市：" + text);
                                mCity = text;
                            }
                        }).show();
                break;

            case R.id.btn_hobby:
                new XPopup.Builder(EditPersonalInfoAc.this).asBottomList("添加爱好", getResources().getStringArray(R.array.hobbys), new OnSelectListener() {
                    @Override
                    public void onSelect(int position, String text) {
                        hobbys.add(text);
                        adapter.addData(text);
                    }
                }).show();
                break;

            case R.id.modfiy_sex:
                new XPopup.Builder(EditPersonalInfoAc.this).asBottomList("是男是女", new String[]{"先生", "女士"}, new OnSelectListener() {
                    @Override
                    public void onSelect(int position, String text) {
                        modfiySex.setText("性别：" + text);
                        mSex = text;
                    }
                }).show();
                break;
        }
    }
}
